package cn.yzking.studentmanagement.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.HashSet;
import java.util.Set;

import cn.yzking.studentmanagement.Student;

public class MyDBHelper extends SQLiteOpenHelper {
    public MyDBHelper(Context context) {
        super(context, "student_management", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE `user`(`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, `username` TEXT NOT NULL, `password` TEXT NOT NULL, UNIQUE (`username`));");
        db.execSQL("CREATE TABLE `student`(`id` INTEGER NOT NULL PRIMARY KEY,`name` TEXT NOT NULL,`phone` TEXT NOT NULL,`sex` TEXT,`birthday` TEXT NOT NULL);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}

    /* 管理员 */
    public boolean userRegister(String username, String password) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("username", username.trim());
        values.put("password", password);
        return db.insert("user", null, values) != -1;
    }

    public boolean userLogin(String username, String password) {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query("user", null, "username=? and password=?", new String[] {username.trim(), password}, null, null, null);
        boolean res = cursor.moveToNext();
        cursor.close();
        return res;
    }
    /* 管理员 */

    /* 学生 */
    public boolean AddStudent(String id, String name, String phone, String sex, String birthday) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("id", id);
        values.put("name", name);
        values.put("phone", phone);
        values.put("sex", sex);
        values.put("birthday", birthday);
        return db.insert("student", null, values) != -1;
    }

    public Set<Student> getAllStudents() {
        Set<Student> studentSet = new HashSet<>();

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query("student", null, null, null, null, null, null);
        while (cursor.moveToNext()) {
            studentSet.add(new Student(cursor.getString(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4)));
        }
        cursor.close();
        db.close();
        return studentSet;
    }

    public boolean updateStudent(String id, String name, String phone, String sex, String birthday) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name", name);
        values.put("phone", phone);
        values.put("sex", sex);
        values.put("birthday", birthday);
        return db.update("student", values, "id=?", new String[] {id}) != 0;
    }

    public boolean deleteStudent(String id) {
        SQLiteDatabase db = this.getWritableDatabase();
        return db.delete("student", "id=?", new String[] {id}) != 0;
    }
    /* 学生 */
}
